clear matrix
clear
set mem 10g


*Set root data directory
local rootdir
cd "`rootdir'"



****Fact 1: within
local round 66
use round`round'_edit, clear
merge 1:1 sector subround region subsample segment substratum fsu hhno using round`round'_hh, keepusing(mpce hhsize mult d* nic*)
drop hq* hx*

cap: drop xfood
gen nfood=0
gen xfood=0
foreach j in grains pulses meat milk oil veg fru sugar bev proc{
replace nfood=nfood+n`j'
replace xfood=xfood+x`j'
}

cap: drop totexp
gen nall=0
gen totexp=0
foreach j in food intox light cloth misc dur{
replace nall=nall+n`j'
replace totexp=totexp+x`j'
}

gen lnfood=log(nfood)
gen lnxfood=log(xfood)
gen lnall=log(nall)
gen lnxall=log(totexp)
gen lnhhsize=log(hhsize)
gen nic=real(nic1)


foreach z in grains pulses veg fru{
gen lnx`z'=log(x`z')
gen ln`z'=log(n`z')
}


set scheme s1mono
foreach z in all food grains pulses veg fru{

local yvar ln`z'
areg `yvar' d*ratio* i.hhsize i.nic, absorb(fsu)
predict ry`z', resid

local yvar lnx`z'
areg `yvar' d*ratio* i.hhsize i.nic, absorb(fsu)
predict rx`z', resid
}

*trim 1% tails
foreach z in all food grains pulses veg fru{
egen pct99rx`z'=pctile(rx`z'), p(99)
egen pct1rx`z'=pctile(rx`z'), p(1)
egen pct99ry`z'=pctile(ry`z'), p(99)
egen pct1ry`z'=pctile(ry`z'), p(1)
gen insample`z'=1 if rx`z'>pct1rx`z' & rx`z'<pct99rx`z' & ry`z'>pct1ry`z'  & ry`z'<pct99ry`z'
}


foreach z in all food grains veg{

foreach type in lowess{
twoway (`type' ry`z' rx`z' if insample`z'==1, title(`z') ytitle("Within log variety") xtitle("Within log expenditure"))
graph save fact1`z'_`type', replace
}
}




*****Fact 2: 
**variance decomposition and graph (for one district?)

local round1 38 
local round2 66

use round`round1'_edit, clear
append using round`round2'_edit


merge m:m region round using mult`round1', keepusing(regioncode mult)
drop _merge
merge m:m region using mult`round2', keepusing(regioncode mult) update
drop _merge

drop region

gen nfood=0

gen regnum=0
*gen fsunum=0

foreach i in grains pulses meat milk oil veg fru sugar bev proc{
gen regnum`i'=0

replace nfood=nfood+n`i'
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
gen presence=0
replace presence=1 if x`i'_`j'~=0 & x`i'_`j'~=.
bysort round regioncode: egen maxpres=max(presence)
replace regnum=regnum+maxpres
replace regnum`i'=regnum`i'+maxpres
drop maxpres presence
}
}

gen nall=nfood+nintox+nlight+ncloth+ndur+nmisc


keep round regioncode sector ngrains nveg nfood nall xfood totexp xgrains xveg regnum* mult
ren totexp xall


merge m:1 round regioncode using pindex_r_revised
keep if _merge==3
drop _merge
ren pindex pindexall
bysort round: summ pindex* x*

foreach j in all food grains veg{
gen rx`j'=x`j'/pindex`j'
gen logx`j'=log(x`j'/pindex`j')
gen logn`j'=log(n`j')
}



***Table 1 First two columns (1983 vs. 2009-2010)
bysort round: summ nfood ngrains nveg regnum regnumgrains regnumveg rxfood rxgrains rxveg [weight=mult]






foreach j in all food grains veg{
egen min`j'=pctile(logx`j'),p(2)
egen max`j'=pctile(logx`j'),p(98)
gen insample`j'=0
replace insample`j'=1 if logx`j'>min`j' & logx`j'<max`j'
}



***new code to generate a kernel density plot of the "over time" dummy in a regression across regions.
preserve


gen y2009=0
replace y2009=1 if round==66

egen rcounter=group(regioncode)
egen rmax=max(rcounter)

levelsof rmax, local(rmax)
foreach j in all food grains veg{
cap: gen rcoef`j'=.
forvalues r=1(1)`rmax'{
reg logn`j' logx`j' y2009 if rcounter==`r'
replace rcoef`j'=_b[y2009] if rcounter==`r'
}
}

duplicates drop rcounter, force

foreach j in all food grains veg{
twoway (kdensity rcoef`j', xtitle("2009-2010 dummy") ytitle(Density) title(`j'))
graph save regtime_`j', replace
}

graph combine regtime_all.gph regtime_food.gph regtime_grains.gph regtime_veg.gph

***Appendix Figure A.2
graph export regtime.pdf, replace
restore

***


local bw 0.95
local type lowess
local z veg
local round1 38
local round2 66
local restrict & regioncode==141
twoway (`type' logn`z' logx`z' if round==`round1' & insample`z'==1 `restrict', bwidth(`bw') lcolor(black) lpattern(solid) title(Coastal Maharashtra `z') ytitle("Log variety") xtitle("Log expenditure (1983 rupees)") legend(order(1 "1983" 2 "2009-2010" ))) (`type' logn`z' logx`z' if round==`round2' & insample`z'==1 `restrict', bwidth(`bw') lcolor(black) lpattern(dash))
graph save cm_veg_time, replace


local bw 0.95
local type lowess
local z all
local round1 38
local round2 66
local restrict
twoway (`type' logn`z' logx`z' if round==`round1' & insample`z'==1 `restrict', bwidth(`bw') lcolor(black) lpattern(solid) title(All India `z') ytitle("Log variety") xtitle("Log expenditure (1983 rupees)") legend(order(1 "1983" 2 "2009-2010" ))) (`type' logn`z' logx`z' if round==`round2' & insample`z'==1 `restrict', bwidth(`bw') lcolor(black) lpattern(dash))
graph save allindia_all_time, replace




local round1 66

use round`round1'_edit, clear


merge m:m region round using mult`round1', keepusing(regioncode mult)
drop _merge

drop region

gen regnum=0

gen nfood=0
foreach i in grains pulses meat milk oil veg fru sugar bev proc{
gen regnum`i'=0
replace nfood=nfood+n`i'
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
gen presence=0
replace presence=1 if x`i'_`j'~=0 & x`i'_`j'~=.
bysort sector regioncode: egen maxpres=max(presence)
replace regnum=regnum+maxpres
replace regnum`i'=regnum`i'+maxpres

drop maxpres presence
}
}

gen nall=nfood+nintox+nlight+ncloth+ndur+nmisc
keep round regioncode sector ngrains nveg nfood nall regnum* xfood totexp xgrains xveg mult
ren totexp xall


merge m:1 round sector regioncode using pindex_rur_revised
keep if _merge==3
drop _merge
ren pindex pindexall
bysort round: summ pindex* x*

foreach j in all food grains veg{
gen rx`j'=x`j'/pindex`j'
gen logx`j'=log(x`j'/pindex`j')
gen logn`j'=log(n`j')
}


***Table 1 Last Two Columns (Rural/Urban 2009-2010)
bysort sector: summ nfood ngrains nveg regnum regnumgrains regnumveg rxfood rxgrains rxveg [weight=mult]




foreach j in all food grains veg{
egen min`j'=pctile(logx`j'),p(2)
egen max`j'=pctile(logx`j'),p(98)
gen insample`j'=0
replace insample`j'=1 if logx`j'>min`j' & logx`j'<max`j'
}



***new code to generate a kernel density plot of the "over time" dummy in a regression across regions.
preserve


gen urban=0
replace urban=1 if sector==2

egen rcounter=group(regioncode)
egen rmax=max(rcounter)

levelsof rmax, local(rmax)
foreach j in all food grains veg{
cap: gen rcoef`j'=.
forvalues r=1(1)`rmax'{
reg logn`j' logx`j' urban if rcounter==`r'
replace rcoef`j'=_b[urban] if rcounter==`r'
}
}

duplicates drop rcounter, force

foreach j in all food grains veg{
twoway (kdensity rcoef`j', xtitle("urban dummy") ytitle(Density) title(`j'))
graph save regurban_`j', replace
}

graph combine regurban_all.gph regurban_food.gph regurban_grains.gph regurban_veg.gph
**Appendix Figure A.3
graph export regurban.pdf, replace

restore

***


local bw 0.95
local type lowess
local z veg
local round1 38
local round2 66
local restrict & regioncode==141
twoway (`type' logn`z' logx`z' if sector==1 & insample`z'==1 `restrict', bwidth(`bw') lcolor(black) lpattern(solid) title(Coastal Maharashtra 2009-2010 `z') ytitle("Log variety") xtitle("Log expenditure (rural rupees)") legend(order(1 "Rural" 2 "Urban" ))) (`type' logn`z' logx`z' if sector==2 & insample`z'==1 `restrict', bwidth(`bw') lcolor(black) lpattern(dash))
graph save cm_veg_ru, replace


local bw 0.95
local type lowess
local z food
local round1 38
local round2 66
local restrict
twoway (`type' logn`z' logx`z' if sector==1 & insample`z'==1 `restrict', bwidth(`bw') lcolor(black) lpattern(solid) title(All India `z') ytitle("Log variety") xtitle("Log expenditure (rural rupees)") legend(order(1 "Rural" 2 "Urban" ))) (`type' logn`z' logx`z' if sector==2 & insample`z'==1 `restrict', bwidth(`bw') lcolor(black) lpattern(dash))
graph save allindia_food_ru, replace





*Figure 1 for paper
local type lowess
graph combine fact1food_`type'.gph fact1grains_`type'.gph fact1veg_`type'.gph allindia_food_time.gph cm_veg_time.gph cm_veg_ru.gph, altshrink
cd `rootdir'
graph export fact12.pdf, replace

